package com.lagou.springboot.controller;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.lagou.springboot.dao.ArticleMapper;
import com.lagou.springboot.service.Article;

@Controller
@RequestMapping("index")
public class IndexController {

    @Autowired
    ArticleMapper articleMapper;

    @RequestMapping
    public String index(Model model, @RequestParam(value = "pageNum", defaultValue = "0") int pageNum) {
        Pageable pageable = PageRequest.of(pageNum, 3);
        Page<Article> all = articleMapper.findAll(pageable);
        model.addAttribute("articles", all.getContent());
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("totalPages", all.getTotalPages());
        return "client/index";
    }
}